// pages/QR/QR.ts
const app = getApp()
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
import { showLoading, hideLoading }  from '../components/loading/loading.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    height:wx.getSystemInfoSync().windowHeight-10,
    linenum:-1,
    worknum:-1,
    hidens:false,
    time:"2023-9-1",
    nums:"",
    hiden2:false,
    synum:0,
    QR1:false,
    QR2:true,
    QRimge:false,
    QR3:true,
    st:false,
    code:-1,
    duration: 1,
    stepList: [{
      name: '开始'
    }, {
      name: '预约'
    }, {
      name: '扫码'
    }, {
      name: '完成'
    }, ],
    stepNum: 1,
    laststep: 1
  },
  collapse(){
    this.setData({
      QR1:false,
      QR2:true,
      QRimge:true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  geiQR(){
    var hidens = this.data.hidens
    var nums= this.data.nums
    if(nums.substr(8).length == 1)
    {
      nums = '*' + nums
    }
    if(nums.length != 0){
      this.setData({
        QR1:true,
        QR2:false,
        QRimge:false
      })
    }
    if(hidens && nums.length != 0){
      var ranges = Math.floor(Math.random() * 100).toString()
      var code = nums + ranges + getApp().globalData.uid 
      console.log(code)
      drawQrcode({
        width: 200, 
        height: 200,
        canvasId: 'myQrcode',
        background:'#ffffff', 
        foreground: '#2bb15e',
        text: code,
        image: {
          dx: 70,
          dy: 70,
          dWidth: 60,
          dHeight: 60
        }
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '请先预约',
      })
    }
  },
  getmynums(){
      var that = this
      var openid = getApp().globalData.uid
      var hidens = this.data.hidens
      if(!hidens){
        getApp().helper({
          url: getApp().globalData.urlpath + '/weixin/getmynums',
          data: {
            openid: openid
          },
          success(res) {
            var e = res.data
            if (res.data.ret == 'True') {
              that.onLoad()
              that.setData({
                hidens:true,
                QR3:false,
                QR2:true,
                QR1:false,
              })
            } else {
              // console.log(res.data);
              that.setData({
                hiden2:true
              })
              wx.showModal({
                title: '提示',
                content: '很遗憾您没有抢到',
              })
            }
          },
          fail() {
            wx.showToast({
              title: '连接服务器失败',
              image: '/static/error.png',
              duration: 1500
            });
          }
        });
      }
  },
  cancelbook(){
    var openid = getApp().globalData.uid
    var that = this
    wx.showModal({
      title:'提示',
      content:'请确认是否预约',
      success:function(res)
      {
        if(res.cancel)
        {
          //点击取消按钮
        }else if(res.confirm)
        {
          getApp().helper({
            url: getApp().globalData.urlpath + '/weixin/cancel',
            data: {
              openid: openid
            },
            success(res) {
              if(res.data.ret == "True"){
                that.onLoad()
                that.setData({
                  hidens:false,
                  nums:"",
                  time:"",
                  QRimge:true,
                  QR2:true,
                  QR1:false,
                  QR3:true
                })
              }
              else if(res.data.ret == "F2"){
                wx.showToast({
                  title: '已签到',
                  icon:"error",
                  duration:1500
                })
                that.setData({
                  QR3:true
                })
              }
                
            },
            fail() {
              wx.showToast({
                title: '连接服务器失败',
                image: '/static/error.png',
                duration: 1500
              });
            }
          });
        }
      }
    })
    
  },
  onLoad() {
    var that = this
    this.onSoekctCallback()
    showLoading()
    getApp().helper({
      url: getApp().globalData.urlpath + '/weixin/checkbook',
      data: {
        openid: getApp().globalData.uid
      },
      success(res) {
        hideLoading()
        if (res.data.ret == 'True') {
          that.setData({
            hidens:true,
            nums:res.data.nums,
            time:res.data.time,
            stepNum:res.data.stepNum,
            st:res.data.st,
            code:res.data.code,
            QR3:false
          })
        } else {
          that.setData({
            hidens:false,
            stepNum:1
          })
        }
        that.setData({
          linenum:res.data.linenums,
          worknum:res.data.worknums,
          synum:res.data.quans
        })
      },
      fail() {
        hideLoading()
        wx.showToast({
          title: '连接服务器失败',
          image: '/static/error.png',
          duration: 15000
        });
      }
    });
    
  },
  
onSoekctCallback() {
  app.globalData.callback = (res) => {
    console.log(res)
    var datas = JSON.parse(res.data)
    this.data.laststep = this.data.stepNum
        if(datas.ret == "F1")
        {
          this.setData({
          linenum:datas.linenum,
          worknum:datas.worknum,
          synum:datas.synum,
          st:datas.st,
          code:datas.code,
          stepNum:datas.stepNum
          })
          if(this.data.stepNum != this.data.laststep && this.data.laststep == 4)
          {
            this.onLoad()
          }
        }
        else if(datas.ret == "F2")
        {
          this.setData({
            linenum:datas.linenum
          })
        }
        else if(datas.ret == "F3")
        {
          this.setData({
            worknum:datas.worknum
          })
        }
  }
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})